#!/bin/sh
#desc:Manage sequence steeps
#type:local
#package:odcommon

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

SEQPATH="/etc/opendomo/sequences"


if ! test -d "$SEQPATH"; then
	mkdir -p $SEQPATH
fi

# List of all supported scripts in /usr/local/bin
POSSIBLECOMMANDS="setport.sh setallports.sh wait.sh play.sh"
for c in $POSSIBLECOMMANDS; do
	# If they exist, and can be executed
	if test -x /usr/local/bin/$c; then
		desc=`grep '#desc' /usr/local/bin/$c | cut -f2 -d:` 2>/dev/null
		if test -z "$desc"; then
			desc="$c"
		fi
		# We add them to the list
		if test -z "$COMMANDS"; then
			COMMANDS="$c:$desc"
		else
			COMMANDS="$COMMANDS,$c:$desc"
		fi
	fi
done

# Is odcontrol properly installed + configured? 
if test -d /etc/opendomo/control; then
	ARGS1="@setport.sh"
	cd /etc/opendomo/control
	for i in `grep way -r * | grep out | cut -f1 -d.`; do
		desc=`cat $i.desc 2>/dev/null `
		if test -z "$desc"; then
			desc="$i"
		fi
		ARGS1="$ARGS1,$i:$desc"
	done
	ARGS2="@setport.sh,on,off"
fi

if ! test -d /etc/opendomo/tags; then
	/usr/local/opendomo/manageTags.sh
fi

cd /etc/opendomo/tags
ARGS1="$ARGS1,@setallports.sh"
for t in *; do
	if test "$t" != "*"; then
		ARGS1="$ARGS1,$t:$t"
	fi
done
ARGS2="$ARGS2,@setallports.sh,on,off"


ARGS1="$ARGS1,@play.sh"
# All the sounds in this directory can be called
cd /usr/share/sounds
ARGS2="$ARGS2,@play.sh,"
for wav in *; do
	ARGS1="$ARGS1,$wav:$wav"
done

ARGS1="$ARGS1,@wait.sh"
ARGS2="$ARGS2,@wait.sh,"
for time in 1 2 3 4 5 6 7 8 9 10 15 20 30 60 120; do
	ARGS1="$ARGS1,$time:$time s"
done




# Graphical Inteface
if ! test -z "$1"; then

	if test -f "$SEQPATH/$1"; then
		# A file is selected
		FILE="$1"
		SEQDESC=`grep 'desc:' $SEQPATH/$FILE | cut -f2 -d:`
	else
		if test "$2" = "addnew"; then
			if test -z "$3"; then
				/usr/local/opendomo/addSequence.sh
				exit 0
			fi
			if test "$1" = "new"; then
				code=`echo $3 | tr A-Z a-z | sed 's/[^a-z0-9]//g'`
				if test -z "$code"; then
					echo "#ERR Invalid name"
					exit 2
				fi
			else
				code="$1"
			fi
			desc=`echo $3 | sed 's/+/ /g'`
			FILE="$code.seq"

			if test -e "$SEQPATH/$FILE"; then
				echo "#ERR File exists"
				exit 2
			fi

			echo "#!/bin/sh
#desc:$desc"  > $SEQPATH/$FILE
			chown admin:admin $SEQPATH/$FILE 2>/dev/null
			chmod +x $SEQPATH/$FILE
		else
			echo "#ERR File not found"
		fi
	fi

	case "$2" in
		"addstep")
			CMD="$3"
			PARAM1="$4"
			PARAM2="$5"
			echo "$CMD $PARAM1 $PARAM2" >> $SEQPATH/$FILE
			echo "#INFO: Sequence step added"
		;;
	esac

	echo "#> Steps in [$SEQDESC]"
	echo "list:`basename $0`	selectable detailed"
	for line in `grep -nv '#' $SEQPATH/$FILE | sed 's/ /:/g'`; do
		lineno=`echo $line | cut -f1 -d:`  
		command=`echo $line | cut -f2 -d:`
		params=`echo $line | cut -f3,4 -d: | sed 's/:/ /g'`
		#echo $line | sed -e "s/^/\t$FILE-/" -e 's/:/\t/' -e 's/$/\tstep/'
		cmddesc=`grep '#desc' /usr/local/bin/$command | cut -f2 -d: |head -n 1`
		if test -z "$cmddesc"; then
			cmddesc=$command
		fi
		par1=`echo $params | cut -f1 -d' '`
		par2=`echo $params | cut -f2 -d' '`
		case "$command" in
			"setport.sh")
				desc=`cat /etc/opendomo/control/$par1.desc` 2>/dev/null
				if test -z "$desc"; then
					desc="$par1"
				fi
				params="$desc [$par2]"
			;;
			"setallports.sh")
				params="$par1 [$par2]"
			;;
		esac
		echo "	$FILE-$lineno	$cmddesc	step	$params"
	done
	echo "actions:"
	echo "	delSequenceStep.sh	Delete step"
	echo
	

	echo "#> Add new step"
	echo "form:`basename $0`	rowform"
	echo "	file	file	hidden	$FILE"
	echo "	action	action	hidden	addstep"
	echo "	command	Command	list[$COMMANDS]	$CMD"
	echo "	param1	Param 1	list[$ARGS1]	$PARAM1"
	echo "	param2	Param 2	list[$ARGS2]	$PARAM2"
	echo "actions:"
	echo "	`basename $0`	Add"
	echo "	manageSequence.sh	Back to list"
	echo	
fi


